﻿function ClientFilterFunc() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    setTimeout("RealFilterFunc('',0, 30, 10, 0, 'Jumppageslistdrugpersons')", 10);
}

function RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    var retValue;
    var ltype = $("#selectlevel").val();
    if (ltype == "0") {
        alert("Chưa chọn cấp địa phương!");
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    var LocationId = $("#_locationsel").val();
    datefrom = document.getElementById("drpDaydatefrom").options[document.getElementById("drpDaydatefrom").selectedIndex].value + '/' + document.getElementById("drpMonthdatefrom").options[document.getElementById("drpMonthdatefrom").selectedIndex].value + '/' + document.getElementById("drpYeardatefrom").options[document.getElementById("drpYeardatefrom").selectedIndex].value;
    dateto = document.getElementById("drpDaydateto").options[document.getElementById("drpDaydateto").selectedIndex].value + '/' + document.getElementById("drpMonthdateto").options[document.getElementById("drpMonthdateto").selectedIndex].value + '/' + document.getElementById("drpYeardateto").options[document.getElementById("drpYeardateto").selectedIndex].value;
    
    retValue = retValue = Portal.SODC.IDIS.ListViewDrugInPrison.GetHtml(ltype, LocationId, datefrom, dateto, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value;
    
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue.replace("ERROR:", ""));
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    document.getElementById("listdrugpersonscontent").innerHTML = retValue;

}

function ClientAddNewDrugInPrisonResetAllField() {
    document.getElementById("drpDaydrcngaynhan1").selectedIndex = 0;
    document.getElementById("drpMonthdrcngaynhan1").selectedIndex = 0;
    document.getElementById("drpYeardrcngaynhan1").selectedIndex = 0;

    document.getElementById("drcdruginprison").value = '';
}



function checkdate(date) {
    var validformat = /^\d{2}\/\d{2}\/\d{4}$/;
    var returnval = false;
    if (!validformat.test(date))
        alert("Nhập sai định dạng ngày tháng.");
    else {
        var dayfield = date.split("/")[0];
        var monthfield = date.split("/")[1];
        var yearfield = date.split("/")[2];
        var dayobj = new Date(yearfield, monthfield - 1, dayfield);
        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (dayobj.getFullYear() != yearfield))
            alert("Vui lòng nhập lại ngày tháng theo định dạng ngày/tháng/năm.");
        else
            returnval = true;
    }
    return returnval;
}

function ClientSideCheckValidate() {
    var ltype = $("#selectlevel").val();
    if (ltype == "0") {
        alert("Chưa chọn cấp địa phương");
        document.getElementById("selectlevel").select();
        return;
    }
    locationid = document.getElementById("_locationsel").options[document.getElementById("_locationsel").selectedIndex].value;
    //sngaynhan = document.getElementById("drcngaynhan1").value;
    sngaynhan = document.getElementById("drpDaydrcngaynhan1").options[document.getElementById("drpDaydrcngaynhan1").selectedIndex].value + '/' + document.getElementById("drpMonthdrcngaynhan1").options[document.getElementById("drpMonthdrcngaynhan1").selectedIndex].value + '/' + document.getElementById("drpYeardrcngaynhan1").options[document.getElementById("drpYeardrcngaynhan1").selectedIndex].value;
    if (sngaynhan == "//") {
        alert("Chưa nhập ngày nhận");
        document.getElementById("drcngaynhan1").focus();
        return;
    }
    
    drugnumber = document.getElementById("drcdruginprison").value;
    if (drugnumber == "") {
        alert("Cần nhập dữ liệu đầy đủ vào những ô bắt buộc!");
        document.getElementById("drcdruginprison").focus();
        return;
    }
    var number = /^[0-9]+/;
    if (!number.test(drugnumber)) {
        alert("Dữ liệu ở ô cần nhập phải là dữ liệu số!");
        document.getElementById("drcdruginprison").select();
        return;
    }
  
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang lưu dữ liệu";
    Retvalue = Portal.SODC.IDIS.AddNewDrugInPrison.ServerSideAddNewDrugInPrison(ltype, locationid, sngaynhan, drugnumber).value;
    if (Retvalue.indexOf("ERROR") != -1) {
        alert(Retvalue.replace("ERROR:", ""));
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    if (Retvalue.indexOf("EXIST") != -1) {
        ConfirmUpdate = Portal.SODC.IDIS.AddNewDrugInPrison.GetConfirmUpdate().value;
        if (confirm(ConfirmUpdate) == true) {
            ClientUpdateAdictsFromAddNews(ltype, locationid, sngaynhan, drugnumber);
        }
        else {
            document.getElementById("divProcessing").innerHTML = "";
            return;
        }
    }
    else {
        alert(Retvalue);
    }
    document.getElementById("divProcessing").innerHTML = "";
    ClientAddNewDrugInPrisonResetAllField();
}

function ShowClientUpdateUrlForm(DrugAddictId, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    Url = Portal.SODC.IDIS.ListViewDrugInPrison.GetShowUpdateForm(DrugAddictId).value;
    if (Url.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(Url.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    window.open(Url, "_blank");
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}

function ClientDeleteDrugAddict(DrugAddictId, belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    Msg = Portal.SODC.IDIS.ListViewDrugInPrison.GetConfirmDeleteMsg().value;
    if (confirm(Msg) == false) return;
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    RetValue = Portal.SODC.IDIS.ListViewDrugInPrison.ServerSideDeleteDrugAddict(DrugAddictId).value;
    if (RetValue.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(RetValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    alert(RetValue);
    RealFilterFunc(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript);
}

function selectLevelTypeforListDrug() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.ListViewDrugInPrison.ServerSideRenderLocationSelect(ltype).value);
}

function Jumppageslistdrugpersons(belongsite, MaxItems, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript) {
    var retValue;
    var ltype = $("#selectlevel").val();
    if (ltype == "0") {
        alert("Chưa chọn cấp địa phương!");
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    var LocationId = $("#_locationsel").val();
    datefrom = document.getElementById("drpDaydatefrom").options[document.getElementById("drpDaydatefrom").selectedIndex].value + '/' + document.getElementById("drpMonthdatefrom").options[document.getElementById("drpMonthdatefrom").selectedIndex].value + '/' + document.getElementById("drpYeardatefrom").options[document.getElementById("drpYeardatefrom").selectedIndex].value;
    dateto = document.getElementById("drpDaydateto").options[document.getElementById("drpDaydateto").selectedIndex].value + '/' + document.getElementById("drpMonthdateto").options[document.getElementById("drpMonthdateto").selectedIndex].value + '/' + document.getElementById("drpYeardateto").options[document.getElementById("drpYeardateto").selectedIndex].value;
    
    retValue = Portal.SODC.IDIS.ListViewDrugInPrison.GetHtml(ltype, LocationId, datefrom, dateto, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value;
    //retValue = Portal.SODC.IDIS.ListViewDrugInPrison.GetHtml(LocationId, AddictName, AddictIdent, AddictOrigin, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript).value;
    if (retValue.indexOf("ERROR") != -1) {
        alert(retValue.replace("ERROR:", ""));
        return;
    }
    document.getElementById("listdrugpersonscontent").innerHTML = retValue;
}

function ClientShowAddnewDrugForm() {
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    Url = Portal.SODC.IDIS.ListViewDrugInPrison.GetShowAddNewsForm().value;
    if (Url.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(Url.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    retValue = window.open(Url, "_blank");
}

function selectLevelTypeforInsertDrug() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.AddNewDrugInPrison.ServerSideRenderLocationSelect(ltype).value);
    $("#SelectHtmlContent").html(Portal.SODC.IDIS.AddNewDrugInPrison.ServerSideRenderHtmlContent(ltype).value);
}

function ClientSideUpdateDrugInPrision(recordid) {
    var ltype = $("#selectlevel").val();
    var LocationId = $("#_locationsel").val();
    if (ltype == "0" || ltype == null) {
        var Ltype_Curent = $("#Ltype_Curent").val();
        ltype = Ltype_Curent;
    }
    if (typeof (LocationId) == 'undefined' || LocationId == null) {
        var LocationId_Curent = $("#Location_Curent").val();
        LocationId = LocationId_Curent;
    }
    sngaynhan = document.getElementById("drpDayupngaynhan1").options[document.getElementById("drpDayupngaynhan1").selectedIndex].value + '/' + document.getElementById("drpMonthupngaynhan1").options[document.getElementById("drpMonthupngaynhan1").selectedIndex].value + '/' + document.getElementById("drpYearupngaynhan1").options[document.getElementById("drpYearupngaynhan1").selectedIndex].value;
    if (sngaynhan == "//") {
        alert("Chưa nhập ngày nhận");
        document.getElementById("upngaynhan1").focus();
        return;
    }
    
    drugnumber = document.getElementById("updrugnumber").value;
    if (drugnumber == "") {
        alert("Chưa nhập tổng số người nghiện!");
        document.getElementById("updrugnumber").focus();
        return;
    }
    var number = /^[0-9]+/;
    if (!number.test(drugnumber)) {
        alert("Tổng số người nghiện phải là dữ liệu số!");
        document.getElementById("updrugnumber").select();
        return;
    }
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang lưu dữ liệu";
    Retvalue = Portal.SODC.IDIS.UpdateDrugInPrison.ServerSideUpdateDrugInPrison(ltype, recordid, LocationId, sngaynhan, drugnumber).value;
    if (Retvalue.indexOf("ERROR") != -1) {
        alert(Retvalue.replace("ERROR:", ""));
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    alert(Retvalue);
    window.close();
}

function ClientExportData() {
    var Ret;
    var ltype = $("#selectlevel").val();
    if (ltype == "0") {
        alert("Chưa chọn cấp địa phương!");
        document.getElementById("divProcessing").innerHTML = "";
        return;
    }
    var LocationId = $("#_locationsel").val();

    datefrom = document.getElementById("drpDaydatefrom").options[document.getElementById("drpDaydatefrom").selectedIndex].value + '/' + document.getElementById("drpMonthdatefrom").options[document.getElementById("drpMonthdatefrom").selectedIndex].value + '/' + document.getElementById("drpYeardatefrom").options[document.getElementById("drpYeardatefrom").selectedIndex].value;
    dateto = document.getElementById("drpDaydateto").options[document.getElementById("drpDaydateto").selectedIndex].value + '/' + document.getElementById("drpMonthdateto").options[document.getElementById("drpMonthdateto").selectedIndex].value + '/' + document.getElementById("drpYeardateto").options[document.getElementById("drpYeardateto").selectedIndex].value;
    
    document.getElementById("divProcessing").innerHTML = "Hệ thống đang xử lý";
    Ret = Portal.SODC.IDIS.ListViewDrugInPrison.ServerExportDrugInPrison(ltype, LocationId, datefrom, dateto).value;
    
    if (Ret.indexOf("ERROR") != -1) {
        document.getElementById("divProcessing").innerHTML = "";
        alert(Ret.replace("ERROR:", ""));
        return;
    }
    document.getElementById("divProcessing").innerHTML = "";
    window.open(Ret, "_blank");
}

function ClientUpdateAdictsFromAddNews(ltype, locationid, sngaynhan, drugnumber) {
    Ret = Portal.SODC.IDIS.AddNewDrugInPrison.ServerSideUpdateDrugInPrison(ltype, locationid, sngaynhan, drugnumber).value;
    if (Ret.indexOf("ERROR") != -1) {
        alert(Ret.replace("ERROR:", ""));
        return;
    }
    alert(Ret);
}

//*************************************************//
